﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Presenters.AppointmentFolder;
using University.Workshop.Model;

namespace University.Workshop.WinApp.AppointmentFolder
{
    public partial class CalendarForm : Form, ICalendarView
    {
        private CalendarPresenter presenter;
        private DateTime todayDate;
        private DateTime currentShowingDate;
        private RegisterAppointmentView registerNewAppointment;
        private List<Model.Appointment> AppoitmentsOfTheDay;


        public CalendarForm()
        {
            InitializeComponent();
            presenter = new CalendarPresenter(this);
            todayDate= DateTime.Now;
            currentShowingDate = todayDate;
            lbTitle.Text = todayDate.ToShortDateString();
            InitializeCalendar();
            presenter.UpdateDayAppointments();
        }

        private void CalendarForm_Load(object sender, EventArgs e)
        {
            presenter.GetCurrentMonth();   
        }

        private void InitializeCalendar()
        {
            CalendarGridView.RowTemplate.Height = 50;
            
        }

        #region ICalendar Implementations
        public void SetCurrentCalendar(ICollection<Model.Calendar> value)
        {
                CalendarGridView.DataSource = value;
        }

        public void ShowMessage(string message)
        {
            MessageBox.Show(message);
        }

        public void SetDayAppointments(ICollection<Model.Appointment> value)
        {
            AppoitmentsOfTheDay = (List<Appointment>) value;
                UpdateListOfAppointments();
        }

        public DateTime CurrentShowingDate
        {
            get
            {
                return currentShowingDate;
            }
            set
            {
                currentShowingDate = value;
                lbTitle.Text = currentShowingDate.ToShortDateString();
            }
        }

        public DateTime TodayDate
        {
            get
            {
                return todayDate;
            }
            set
            {
                todayDate = value;
            }
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNextMonth_Click(object sender, EventArgs e)
        {
            presenter.GetNextMonth();
        }

        private void btnPreviousMonth_Click(object sender, EventArgs e)
        {
            presenter.GetPreviousMonth();
        }

        private void CalendarGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // presenter.UpdateSelectedDay(CalendarGridView.CurrentCell.Value.ToString());
        }

        private void CalendarGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
                presenter.UpdateSelectedDay(CalendarGridView.CurrentCell.Value.ToString());
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment800.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "8:00");
        }

        private void OpenNewAppointment(string date, string time)
        {
            registerNewAppointment = new RegisterAppointmentView(date, time);
            registerNewAppointment.ShowDialog();
            UpdateListOfAppointments();
        }

        private void UpdateListOfAppointments() 
        {
            Appointment800.Text = "Nueva Cita";
            Appointment900.Text = "Nueva Cita";
            Appointment1000.Text = "Nueva Cita";
            Appointment1100.Text = "Nueva Cita";
            Appointment1200.Text = "Nueva Cita";
            Appointment1500.Text = "Nueva Cita";
            Appointment1600.Text = "Nueva Cita";
            Appointment1700.Text = "Nueva Cita";
            Appointment1800.Text = "Nueva Cita";
            Appointment1900.Text = "Nueva Cita";

            foreach (var app in AppoitmentsOfTheDay)
            {
                if (app.Time == "8:00")
                    Appointment800.Text = "Cita Paciente:"+app.PatientId;
                if (app.Time == "9:00")
                    Appointment900.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "10:00")
                    Appointment1000.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "11:00")
                    Appointment1100.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "12:00")
                    Appointment1200.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "15:00")
                    Appointment1500.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "16:00")
                    Appointment1600.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "17:00")
                    Appointment1700.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "18:00")
                    Appointment1800.Text = "Cita Paciente:" + app.PatientId;
                if (app.Time == "19:00")
                    Appointment1900.Text = "Cita Paciente:" + app.PatientId;
            }
        }

        private void ModifyAppointment()
        { }

        private void Appointment900_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment900.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "9:00");
        }

        private void Appointment1000_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1000.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "10:00");
        }

        private void Appointment1100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1100.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "11:00");
        }

        private void Appointment1200_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1200.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "12:00");
        }

        private void Appointment1500_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1500.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "15:00");
        }

        private void Appointment1600_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1600.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "16:00");
        }

        private void Appointment1700_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1700.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "17:00");
        }

        private void Appointment1800_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1800.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "18:00");
        }

        private void Appointment1900_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Appointment1900.Text.Equals("Nueva Cita"))
                OpenNewAppointment(lbTitle.Text, "19:00");
        }   
    }
}
